﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace WeForms
{
    [ToolboxItem(false)]
    public class WeTabSideBarButton : WeTabSideBarControl
    {
        [Flags]
        enum WeTabButtonState
        {
            Normal = 0,
            Pressed = 1,
            Checked = 2,
        }

        private WeTabButtonState _state;
        private bool _checkOnClick;

        [DefaultValue(true)]
        public virtual bool Checked
        {
            get { return GetState(WeTabButtonState.Checked); }
            set
            {
                if (Checked == value) return;
                SetState(WeTabButtonState.Checked, value);
                OnCheckedChanged(EventArgs.Empty);
            }
        }

        [DefaultValue(false)]
        public virtual bool CheckOnClick
        {
            get { return _checkOnClick; }
            set
            {
                if (_checkOnClick == value) return;
                _checkOnClick = value;
            }
        }

        private Image _image;
        public Image Image
        {
            get { return _image; }
            set
            {
                if (_image == value) return;
                _image = value;
                Invalidate();
            }
        }

        [Browsable(false)]
        public bool Pressed
        {
            get { return GetState(WeTabButtonState.Pressed); }
            private set
            {
                if (Pressed == value) return;
                SetState(WeTabButtonState.Pressed, value);
            }
        }

        private bool GetState(WeTabButtonState state)
        {
            return (_state & state) != 0;
        }  
        
        private void SetState(WeTabButtonState state, bool value)
        {
            if (value)
            {
                State |= state;
            }
            else
            {
                State &= ~state;
            }
        }


        private WeTabButtonState State
        {
            get
            {
                return _state;
            }
            set
            {
                _state = value;
                Invalidate();
            }
        }

        public override Size GetPreferredSize(Size proposedSize)
        {
            return WeTabTheme.ButtonSize;
        }

        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, WeTabTheme.ButtonSize.Width, WeTabTheme.ButtonSize.Height, specified);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            Console.WriteLine("MouseDown");

            if (e.Button != MouseButtons.Left) return;
            Capture = true;
            Pressed = true;
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            Console.WriteLine("MouseUp {0} | {1} | {2}", Bounds, e.Location, Bounds.Contains(e.Location));

            Capture = false;
            Pressed = false;
        }

        protected override void OnClick(EventArgs e)
        {
            // TODO: Mono doesn't raise click on wetab
            Console.WriteLine("Click");
            if (_checkOnClick)
            {
                Checked = !Checked;
            }
            base.OnClick(e);
        }


        protected virtual void OnCheckedChanged(EventArgs e)
        {
            if (CheckedChanged != null)
                CheckedChanged(this, e);
        }
        public event EventHandler CheckedChanged;

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);

            pevent.Graphics.Clear(WeTabTheme.SideBarBackground);

            int index = 0;
            if (Pressed)
            {
                index = 1;
            }
            else if (Checked)
            {
                index = 2;
            }
            else
            {
                index = 0;
            }

            Rectangle rect = new Rectangle(0, 0, Width, Height);

            pevent.Graphics.DrawImage(WeTabTheme.Button[index], rect, new Rectangle(Point.Empty, WeTabTheme.Button[index].Size),
                                      GraphicsUnit.Pixel);

            rect.X += Padding.Left;
            rect.Y += Padding.Top;
            rect.Width -= Padding.Horizontal;
            rect.Height -= Padding.Vertical;

            using (SolidBrush brush = new SolidBrush(WeTabTheme.ButtonTextColors[index]))
            {
                pevent.Graphics.DrawString(Text.ToUpper(), WeTabTheme.ButtonFont, brush, rect,
                                           WeTabTheme.ButtonTextFormat);
            }

            if(_image != null)
            {
                pevent.Graphics.DrawImage(_image, new Rectangle(WeTabTheme.ButtonImagePosition, _image.Size), new Rectangle(Point.Empty, _image.Size),
                                          GraphicsUnit.Pixel);
            }
            
        }
    }


}
